package com.debao.web.controller.debao;

import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.debao.common.core.domain.entity.SysUser;
import com.debao.framework.web.service.SysRegisterService;
import com.debao.system.service.ISysUserService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.debao.common.annotation.Log;
import com.debao.common.core.controller.BaseController;
import com.debao.common.core.domain.AjaxResult;
import com.debao.common.enums.BusinessType;
import com.debao.system.domain.ExcellentStudent;
import com.debao.system.service.IExcellentStudentService;
import com.debao.common.utils.poi.ExcelUtil;
import com.debao.common.core.page.TableDataInfo;

/**
 * 优秀学员Controller
 *
 * @author DeBao
 * @date 2024-08-22
 */
@RestController
@RequestMapping("/debao/student")
public class ExcellentStudentController extends BaseController {
    @Autowired
    private IExcellentStudentService excellentStudentService;


    @Resource
    private ISysUserService userService;

    @Resource
    private SysRegisterService registerService;

    /**
     * 查询优秀学员列表
     */
    @PreAuthorize("@ss.hasPermi('debao:student:list')")
    @GetMapping("/list")
    public TableDataInfo list(ExcellentStudent excellentStudent) {
        startPage();
        List<ExcellentStudent> list = excellentStudentService.selectExcellentStudentList(excellentStudent);
        return getDataTable(list);
    }

    /**
     * 导出优秀学员列表
     */
    @PreAuthorize("@ss.hasPermi('debao:student:export')")
    @Log(title = "优秀学员", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, ExcellentStudent excellentStudent) {
        List<ExcellentStudent> list = excellentStudentService.selectExcellentStudentList(excellentStudent);
        ExcelUtil<ExcellentStudent> util = new ExcelUtil<ExcellentStudent>(ExcellentStudent.class);
        util.exportExcel(response, list, "优秀学员数据");
    }

    /**
     * 获取优秀学员详细信息
     */
    @PreAuthorize("@ss.hasPermi('debao:student:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return success(excellentStudentService.selectExcellentStudentById(id));
    }

    /**
     * 新增优秀学员
     */
    @PreAuthorize("@ss.hasPermi('debao:student:add')")
    @Log(title = "优秀学员", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody ExcellentStudent excellentStudent) {
        // 获取用户是否已存在【手机号是否被注册】
        boolean isExist = userService.isExistUser(excellentStudent.getPhone());
        // 用户不存在
        if (!isExist) {
            // 开始注册用户 | 注册成功返回
            excellentStudent.setUserId(registerService.register(excellentStudent.getName(), excellentStudent.getPhone()));
            return toAjax(excellentStudentService.insertExcellentStudent(excellentStudent));
        } else {
            // 用户已存在
            SysUser user = userService.getUserByPhone(excellentStudent.getPhone());
            if (user != null) {
                excellentStudent.setUserId(user.getUserId());
                return toAjax(excellentStudentService.insertExcellentStudent(excellentStudent));
            }
        }
        return error("新增优秀学员失败");
    }

    /**
     * 修改优秀学员
     */
    @PreAuthorize("@ss.hasPermi('debao:student:edit')")
    @Log(title = "优秀学员", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody ExcellentStudent excellentStudent) {
        // 用户已存在
        SysUser user = userService.getUserByPhone(excellentStudent.getPhone());
        if (user != null) {
            excellentStudent.setUserId(user.getUserId());
            return toAjax(excellentStudentService.updateExcellentStudent(excellentStudent));
        }
        return error("您输入的手机号还未注册，请添加学员");
    }

    /**
     * 删除优秀学员
     */
    @PreAuthorize("@ss.hasPermi('debao:student:remove')")
    @Log(title = "优秀学员", businessType = BusinessType.DELETE)
    @DeleteMapping("/{id}")
    public AjaxResult remove(@PathVariable Long id) {
        return toAjax(excellentStudentService.deleteExcellentStudentById(id));
    }
}
